﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using cludg.Web.Core.Controllers;
using cludg.Services.Content;
using cludg.Web.RateContentModule.Services;
using cludg.Infrastructure.Core.Attributes;
using cludg.Web.Core.Security;
using cludg.Web.RateContentModule.ViewModels;

namespace cludg.Web.RateContentModule.Controllers
{
    public class RateController : BaseController
    {
        [Dependency]
        public IStoryService StoryService { get; set; }
        [Dependency]
        public RateService RateService { get; set; }

        [PermissionRequierd("RateStory")]
        public ActionResult Rate(ViewModels.RateModel model)
        {
            InitModel(model);

            var story = StoryService.ById(model.StoryId);

            if (story == null)
                return RedirectToRoute("Default");

            RateService.Rate(GetCurrentUser(false), story, model.Positive ? +1 : -1);

			return Redirect(this.HttpContext.Request.UrlReferrer.AbsoluteUri);

            //return RedirectToRoute("Story", new { Id = model.StoryId });
        }

        public ActionResult StoriesTopByRate()
        {
            var model = CreateModel<StoriesTopModel>(
                delegate(StoriesTopModel m)
                {
                    m.StoriesTop = RateService.TopRatedStories(10);
                });
            return PartialView("StoriesTop", model);
        }
    }
}
